#!/usr/bin/env python

from wx import Dialog, BoxSizer, StaticBox, StaticBoxSizer, TextCtrl, ListBox, Button, StaticText, CheckBox, StaticLine, Point, Size, Colour
from wx import VERTICAL, HORIZONTAL, LB_EXTENDED, EVT_LISTBOX_DCLICK, EVT_KEY_UP, LEFT, ALL, EXPAND, ALIGN_CENTER, TOP, BOTTOM, RIGHT, EVT_BUTTON, EVT_CHECKBOX
from ConfigParser import RawConfigParser
from time import sleep


class SettingsDialog(Dialog):

  def __init__(self, parent):
    from client import getSetting
    super(SettingsDialog, self).__init__(parent=parent,
        title='Settings', size=(400, 500))


    def loadFromSettings():
      self.settingsDict = {}

      self.settingsDict['auctionItemsFromFile'] = CheckBox(self, -1, 'Auction items in shop located in auctionsFile.txt (advanced)', (15,30))
      self.settingsDict['auctionItemsFromFile'].SetValue(getSetting('def', 'auctionItemsFromFile'))
      self.settingsDict['extrabankactions'] = CheckBox(self, -1, 'Perform extra bank actions after collecting interest', (15,30))
      self.settingsDict['extrabankactions'].SetValue(getSetting('def', 'extrabankactions'))
      self.settingsDict['eatomelette'] = CheckBox(self, -1, 'Eat omelette after aquiring it', (15,30))
      self.settingsDict['eatomelette'].SetValue(getSetting('def', 'eatomelette'))
      self.settingsDict['itemsIntoShopAfter'] = CheckBox(self, -1, 'Put all items in inventory into shop after dailies', (15,30))
      self.settingsDict['itemsIntoShopAfter'].SetValue(getSetting('def', 'itemsIntoShopAfter'))

      self.settingsDict['wellAmount'] = TextCtrl(self, 10, str(getSetting('def', 'wellamount')), size=(60,25))
      self.settingsDict['wellWish'] = TextCtrl(self, 10, str(getSetting('def', 'wellwish')), size=(140,25))

      self.settingsDict['buystockmax'] = TextCtrl(self, 10, str(getSetting('def', 'buystockmax')), size=(60,25))
      self.settingsDict['sellstockmin'] = TextCtrl(self, 10, str(getSetting('def', 'sellstockmin')), size=(60,25))
      self.settingsDict['pageMin'] = TextCtrl(self, 10, str(getSetting('def', 'pagemin')), size=(60,25))
      self.settingsDict['pageMax'] = TextCtrl(self, 10, str(getSetting('def', 'pagemax')), size=(60,25))
      self.settingsDict['betweendailiesmin'] = TextCtrl(self, 10, str(getSetting('def', 'betweendailiesmin')), size=(60,25))
      self.settingsDict['betweendailiesmax'] = TextCtrl(self, 10, str(getSetting('def', 'betweendailiesmax')), size=(60,25))
      self.settingsDict['longerDelayMin'] = TextCtrl(self, 10, str(getSetting('def', 'longerdelaymin')), size=(60,25))
      self.settingsDict['longerDelayMax'] = TextCtrl(self, 10, str(getSetting('def', 'longerDelayMax')), size=(60,25))
      self.settingsDict['depositIfOver'] = TextCtrl(self, 10, str(getSetting('def', 'depositifover')), size=(60,25))
      self.settingsDict['depositMultiplier'] = TextCtrl(self, 10, str(getSetting('def', 'depositmultiplier')), size=(60,25))


    loadFromSettings()
    vbox = BoxSizer(VERTICAL)

    hbox2 = BoxSizer(HORIZONTAL)
    okButton = Button(self, label='Ok')
    hbox2.Add(okButton)

    r1 = BoxSizer(HORIZONTAL)
    r1.Add(StaticText(self, -1, 'Page Delay', size=(150,25)))
    r1.Add(self.settingsDict['pageMin'], border=10)
    r1.Add(self.settingsDict['pageMax'], border=10)
    r1.Add(StaticText(self, -1, 's', size=(150,25)))

    r2 = BoxSizer(HORIZONTAL)
    r2.Add(StaticText(self, -1, 'Between Dailies', size=(150,25)))
    r2.Add(self.settingsDict['betweendailiesmin'], border=10)
    r2.Add(self.settingsDict['betweendailiesmax'], border=10)
    r2.Add(StaticText(self, -1, 's', size=(150,25)))

    r3 = BoxSizer(HORIZONTAL)
    r3.Add(StaticText(self, -1, 'Longer Page Delay', size=(150,25)))
    r3.Add(self.settingsDict['longerDelayMin'], border=10)
    r3.Add(self.settingsDict['longerDelayMax'], border=10)
    r3.Add(StaticText(self, -1, 's', size=(150,25)))

    r4 = BoxSizer(HORIZONTAL)
    r4.Add(StaticText(self, -1, 'Stocks MAX to pay before giving up', size=(150,50)))
    r4.Add(self.settingsDict['buystockmax'], border=10)
    r4.Add(StaticText(self, -1, 'np', size=(150,25)))

    r5 = BoxSizer(HORIZONTAL)
    r5.Add(StaticText(self, -1, 'Minimum to sell stocks for', size=(150,50)))
    r5.Add(self.settingsDict['sellstockmin'], border=10)
    r5.Add(StaticText(self, -1, 'np', size=(150,25)))



    vbox.Add(StaticText(self, -1, 'Time between pages'), flag = ALIGN_CENTER)
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(r1)
    vbox.Add(r2)
    vbox.Add(r3)
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(StaticText(self, -1, 'Stocks Settings'), flag = ALIGN_CENTER)
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(r4)
    vbox.Add(r5)

    r5 = BoxSizer(HORIZONTAL)
    r5.Add(StaticText(self, -1, 'Deposit some multiple of ', size=(150,50)))
    r5.Add(self.settingsDict['depositMultiplier'], border=10)
    r5.Add(StaticText(self, -1, 'NP to get below' ))
    r5.Add(self.settingsDict['depositIfOver'], border=10)
    r5.Add(StaticText(self, -1, 'NP' ))
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(StaticText(self, -1, 'Banking Settings'), flag = ALIGN_CENTER)
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(self.settingsDict['extrabankactions'])
    vbox.Add(r5)

    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(StaticText(self, -1, 'Wish Settings'), flag = ALIGN_CENTER)
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    r7 = BoxSizer(HORIZONTAL)
    r7.Add(StaticText(self, -1, 'Donation amount ', size=(120,50)))
    r7.Add(self.settingsDict['wellAmount'], border=10)
    r7.Add(StaticText(self, -1, 'NP    Wish', size=(70,50)))
    r7.Add(self.settingsDict['wellWish'], border=10)
    vbox.Add(r7)



    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(StaticText(self, -1, 'Other Settings'), flag = ALIGN_CENTER)
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))

    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))
    vbox.Add(self.settingsDict['eatomelette'])
    vbox.Add(self.settingsDict['itemsIntoShopAfter'])
    vbox.Add(self.settingsDict['auctionItemsFromFile'])
    vbox.Add(StaticLine(self, -1, (0,0), (3000,1)))



    vbox.Add(hbox2, flag= ALIGN_CENTER|TOP|BOTTOM, border=10)

    self.SetSizer(vbox)

    EVT_CHECKBOX(self, self.settingsDict['extrabankactions'].GetId(), self.blurBanking)

    self.blurBanking()
    okButton.Bind(EVT_BUTTON, self.OnOK)
    self.username = None
    self.password = None
    self.proxy = None
    self.authenticated = False

  def blurBanking(self, event=None):
    enabled = self.settingsDict['extrabankactions'].GetValue()
    self.settingsDict['depositIfOver'].Enable(enabled)
    self.settingsDict['depositMultiplier'].Enable(enabled)


  def getAllSettings(self):
    retDict = {}
    for e in self.settingsDict:
      retDict[e] = self.settingsDict[e].GetValue()
    return retDict


  def OnOK(self, event):
    self.Close()


